home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / SingletonPattern.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  726 b   |  32 lines

  1. //: C25:SingletonPattern.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Singleton {
  10.   static Singleton s;
  11.   int i;
  12.   Singleton(int x) : i(x) { }
  13.   void operator=(Singleton&);
  14.   Singleton(const Singleton&);
  15. public:
  16.   static Singleton& getHandle() {
  17.     return s;
  18.   }
  19.   int getValue() { return i; }
  20.   void setValue(int x) { i = x; }
  21. };
  22.  
  23. Singleton Singleton::s(47);
  24.  
  25. int main() {
  26.   Singleton& s = Singleton::getHandle();
  27.   cout << s.getValue() << endl;
  28.   Singleton& s2 = Singleton::getHandle();
  29.   s2.setValue(9);
  30.   cout << s.getValue() << endl;
  31. } ///:~
  32.